package es.project.sien7e.main.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.validation.Valid;

import org.springframework.stereotype.Service;

import es.project.sien7e.main.PMF;
import es.project.sien7e.main.model.Menu;

@Service
public class MenuServiceImpl implements MenuService {
	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getMenus() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setOrdering("orden asc,id asc");
		return (List<Menu>) query.execute();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getMenusPadres() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setFilter("menu == ''");
		query.setOrdering("orden asc,id asc");
		return (List<Menu>) query.execute();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Long getMenu(String titulo) throws SQLException {
		Menu menu = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setFilter("titulo == param");
		query.setOrdering("orden asc, id asc");
		query.declareParameters("String param");
		List<Menu> menus = (List<Menu>) query.execute(titulo);
		for (Menu m : menus)
			if (m != null)
				menu = m;
		return menu == null ? null : menu.getMenu() == null ? menu.getMenu()
				: menu.getMenu();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getListaMenus() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setOrdering("id asc");
		return (List<Menu>) query.execute();
	}

	@Override
	public Menu grabaMenu(@Valid Menu obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Menu obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Menu insertaMenu(@Valid Menu obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Menu obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Integer borrarMenu(@Valid Menu obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Integer val = 0;
		try {
			pm.deletePersistent(obj);
			val = 1;
		} finally {
			pm.close();
		}
		return val;
	}

	@SuppressWarnings("unchecked")
	@Override
	public HashMap<Long, String> getMenusPadres(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		String filter = "menu == null";
		query.setFilter(filter);
		query.setOrdering("orden asc");
		List<Menu> list = (List<Menu>) query.execute();
		Iterator<Menu> it = list.iterator();
		HashMap<Long, String> map = new HashMap<Long, String>();
		map.put(new Long(0), "");
		while (it.hasNext()) {
			Menu menu = (Menu) it.next();
			if (!id.equals(menu.getId()))
				map.put(menu.getId(), menu.getTitulo());
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getMenuPrincipal(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setFilter("menu == null && borrado == false");
		query.setOrdering("orden asc");
		return (List<Menu>) query.execute();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> getMenuSecundario(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Menu.class);
		query.setFilter("menu == padreParam && borrado == false");
		query.setOrdering("orden asc");
		query.declareParameters("Long padreParam");
		return (List<Menu>) query.execute(id);
	}
}
